/****************************************************
    Author:            龙之介
    CreatTime:    #CreatTime#
    Description:     Nothing
*****************************************************/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.UI;
using ZYKTool;
using System.Linq;
using DG.Tweening;


namespace LongZhiJie
{
    public enum EnumGun
    {
        Gun1=0,
        Gun2=1,
        Gun3=2,
       // Gun4=3
    }
    public class GunManger : MonoBehaviour
    {
        #region 常量


        public float offset;
        public GameObject[] projectile;
        public GameObject shotEffect;




        private float _timeBtwShots;
        private int _升级枪的次数 = 0;
        private Dictionary<EnumGun, GunItem> _GunsDic;
        #endregion

        #region 事件
        #endregion

        #region 字段
        #endregion


        #region 属性
        public GunItem CurrentGunItem
        {
            get;
            private set;
        }
        #endregion

        protected AudioSource _AudioSource;
        protected void PlaySound()
        {
            string name = "射击BGM";

            AudioClip clip = Resources.Load<AudioClip>(name);
            _AudioSource.clip = clip;
            _AudioSource.PlayOneShot(clip);
        }
        #region 方法 

        private void Init()
        {
            _AudioSource = transform.GetOrAddComponent<AudioSource>();
            _GunsDic = new Dictionary<EnumGun, GunItem>();
           //_GunsDic[EnumGun.Gun1]=
            int index = 0;
            //初始化枪
            foreach (EnumGun gun in Enum.GetValues(typeof(EnumGun)))
            {
                Transform child = transform.Find(gun.ToString());
                GunItem gunItem = child.GetOrAddComponent<GunItem>();
                _GunsDic.Add(gun, gunItem);
                if (gun != EnumGun.Gun1)
                {
                    child.gameObject.SetActive(false);
                }
                else
                {
                    CurrentGunItem = gunItem;
                }
                switch (index + 1)
                {
                    case 1: gunItem.Gun基本属性 = new Gun1(); gunItem.Gun基本属性.EnumGun = EnumGun.Gun1; break;
                    case 2: gunItem.Gun基本属性 = new Gun2(); gunItem.Gun基本属性.EnumGun = EnumGun.Gun2; break;
                    case 3: gunItem.Gun基本属性 = new Gun3(); gunItem.Gun基本属性.EnumGun = EnumGun.Gun3; break;
                    //case 4: gunItem.Gun基本属性 = new Gun4(); gunItem.Gun基本属性.EnumGun = EnumGun.Gun4; break;
                }
                Transform shootPos = gunItem.transform.Find("Shot Point");


                gunItem.Gun基本属性.Name = TapTapStaticValue.Guns属性[index].name;
                gunItem.Gun基本属性.Damage = TapTapStaticValue.Guns属性[index].damage;
                gunItem.Gun基本属性.Count = TapTapStaticValue.Guns属性[index].Count;
                gunItem.Gun基本属性.StartTimeBtwShots = TapTapStaticValue.Guns属性[index].startTimeBtwShots;

                gunItem.Gun基本属性.Init(projectile[index], shotEffect, shootPos);
                index++;
            }


           
        }

        private void Attack()
        {
            PlaySound();
            Camera.main.DOShakePosition(0.05f);
            _timeBtwShots = CurrentGunItem.Gun基本属性.StartTimeBtwShots;//控制射速

           // Debug.LogError(_timeBtwShots);
            CurrentGunItem.Attack();//调用射击方法
        }
    
        private void ChangeGun(EnumGun gun)
        {
           
          //  if((int)gun>_升级枪的次数)
           // {
              //  Debug.LogError("未解锁枪");
              //  return;
          //  }
            if (_GunsDic.ContainsKey(gun))
            {

                CurrentGunItem.gameObject.SetActive(false);
                CurrentGunItem = _GunsDic[gun];
                CurrentGunItem.gameObject.SetActive(true);

            }
            else
            {
                Debug.LogError("切枪失败");
            }

        }

        public string UpdateGun()
        {
           // Debug.LogError("升级");
          //  _升级枪的次数++;
           // if(_升级枪的次数>2)
            {
                
                return CurrentGunItem.UpdateGun();

            }
           // else
            //{
          //      return "获得新枪,按" + (_升级枪的次数+1).ToString()+"切换";
          //  }
        }

        #endregion

        #region Unity回调
        private void Start()
        {
            Init();
        }
        private void Update()
        {
            #region 切换枪
            if (Input.GetKeyDown(KeyCode.Alpha1))
            {
                ChangeGun(EnumGun.Gun1);
            }
            else if(Input.GetKeyDown(KeyCode.Alpha2))
            {
                ChangeGun(EnumGun.Gun2);
            }
            else if (Input.GetKeyDown(KeyCode.Alpha3))
            {
                ChangeGun(EnumGun.Gun3);
            }
            else if (Input.GetKeyDown(KeyCode.Alpha4))
            {
             //   ChangeGun(EnumGun.Gun4);
            }

            #endregion
            #region 枪头跟随鼠标移动
            Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - CurrentGunItem.transform.position;
            float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
            CurrentGunItem.transform.rotation = Quaternion.Euler(0f, 0f, rotZ + offset);
            #endregion
            #region 射击

            if (_timeBtwShots <= 0)
            {
                if (Input.GetMouseButton(0))
                {
                    Attack();
                }
            }
            else
            {
                _timeBtwShots -= Time.deltaTime;
            }
            #endregion

            //升级
            if(Input.GetKeyDown(KeyCode.E))
            UpdateGun();
        }
        #endregion

        #region 事件回调
        #endregion

        #region 帮助方法
        #endregion

    }
}